package com.easylinkin.linkappapi.quality.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.common.utils.async.AsyncUtil;
import com.easylinkin.linkappapi.operatelog.LogHelper;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogOperateType;
import com.easylinkin.linkappapi.quality.dto.QualityPositionDTO;
import com.easylinkin.linkappapi.quality.entity.QualityPosition;
import com.easylinkin.linkappapi.quality.entity.vo.QualityPositionVo;
import com.easylinkin.linkappapi.quality.service.QualityPositionService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: kan yuanfeng
 * @Date: 2020/11/04 11:42
 * @Description: 部位管理
 */
@Slf4j
@RestController
@RequestMapping("qualityPosition")
@Api(tags = "部位管理")
public class QualityPositionController {

    @Autowired
    private QualityPositionService qualityPositionService;

    @Resource
    private LinkappUserContextProducer linkappUserContextProducer;

    /**
     * @Description: 增加部位
     * @author kan yuanfeng
     * @date 2020/11/04 11:42
     */
    @PostMapping
    @ApiOperation("增加部位")
    public RestMessage insert(@RequestBody QualityPosition qualityPosition) {
        //todo 参数验证
        qualityPositionService.insert(qualityPosition);
        return RestBuilders.successBuilder().build();
    }

    /**
     * @Description: 删除部位（包含批量删除）
     * @author kan yuanfeng
     * @date 2020/11/04 11:42
     */
    @DeleteMapping
    @ApiOperation("删除部位（包含批量删除）")
    @CommonOperateLogAnnotate(module = LogModule.SAFE_MANAGEMENT, desc = "")
    public RestMessage delBatch(@RequestBody QualityPosition qualityPosition) {
        Assert.notEmpty(qualityPosition.getIds(), "id不能为空");
        // 记录日志详情
        List<String> positions = new ArrayList<>();
        qualityPosition.getIds().forEach(p -> {
            QualityPosition qualityPositionServiceById = qualityPositionService.getById(p);
            positions.add(qualityPositionServiceById.getFullName());
        });
        if (CollectionUtil.isNotEmpty(positions)) {
            LogHelper
                    .setContent(LogOperateType.SAFE_QUALITY_POSITION_DELETE, StringUtil.join(positions, ","));
        }
        qualityPositionService.delBatch(qualityPosition.getIds());
        return RestBuilders.successBuilder().build();
    }

    /**
     * @Description: 编辑部位
     * @author kan yuanfeng
     * @date 2020/11/04 11:42
     */
    @PutMapping
    @ApiOperation("编辑部位")
    public RestMessage updateById(@RequestBody QualityPosition qualityPosition) {
        Assert.notNull(qualityPosition.getId(), "id不能为空");
        qualityPositionService.updateOne(qualityPosition);
        return RestBuilders.successBuilder().build();
    }

    /**
     * @Description: 根据id查询部位详情
     * @author kan yuanfeng
     * @date 2020/11/04 11:42
     */
    @GetMapping("{id}")
    @ApiOperation("根据id查询部位详情")
    public RestMessage findById(@PathVariable("id") Long id) {
        Assert.notNull(id, "id不能为空");
        QualityPosition qualityPosition = qualityPositionService.getById(id);
        return RestBuilders.successBuilder().data(qualityPosition).build();
    }

    /**
     * @Description: 根据条件，分页(不分页)查询
     * @author kan yuanfeng
     * @date 2020/11/04 11:42
     */
    @PostMapping("list")
    @ApiOperation("根据条件，分页(不分页)查询")
    public RestMessage queryList(@RequestBody QualityPositionDTO qualityPositionDTO) {
//        update 20220901 兼容质量部位查询 没传type默认是查质量
        if (null == qualityPositionDTO.getType()) {
            qualityPositionDTO.setType(1);
        }
        List<QualityPositionDTO> record = qualityPositionService.queryList(qualityPositionDTO);
        return RestBuilders.successBuilder().data(record).build();
    }

    /**
     * 部位Excel导入
     *
     * @param file
     * @return
     */
    @PostMapping("excelImport/{type}")
    @ResponseBody
    public RestMessage excelImport(@NotNull @RequestPart("file") MultipartFile file, @PathVariable("type") Integer type) {
        //获取当前时间的时间
        log.info("部位excel数据导入");
        String key = "qualityPosition:" + DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN);
        LinkappUser linkappUser = linkappUserContextProducer.getNotNullCurrent();
        AsyncUtil.submitTask(key, () -> qualityPositionService.excelImport(file, type,linkappUser));
        return RestBuilders.successBuilder().data(key).build();
    }

    /**
     * 实测实量部位树带实测实量状态
     *
     * @param qualityPositionDTO 部位dto入参
     * @return 统一出参
     */
    @PostMapping("/queryListWithMeasureNum")
    @ApiOperation("实测实量部位树带实测实量状态")
    public RestMessage queryListWithMeasureNum(@RequestBody QualityPositionDTO qualityPositionDTO) {
        log.info("实测实量部位树带实测实量状态");
        return qualityPositionService.queryListWithMeasureNum(qualityPositionDTO);
    }

    /**
     * 混凝土强度检测部位树带测量状态
     *
     * @param qualityPositionDTO 部位dto入参
     * @return 统一出参
     */
    @PostMapping("/queryListWithConcreteNum")
    @ApiOperation("混凝土强度检测部位树带测量状态")
    public RestMessage queryListWithConcreteNum(@RequestBody QualityPositionDTO qualityPositionDTO) {
        log.info("混凝土强度检测部位树带测量状态");
        return qualityPositionService.queryListWithConcreteNum(qualityPositionDTO);
    }

    /**
     * 根据条件查询部位
     *
     * @param qualityPositionVo 入参vo
     * @return 统一出参
     */
    @PostMapping("/queryByParam")
    @ApiOperation("根据条件查询部位")
    public RestMessage queryByParam(@RequestBody QualityPositionVo qualityPositionVo) {
        log.info("根据条件查询部位");
        return qualityPositionService.queryByParam(qualityPositionVo);
    }
}

